home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / datatypes / dfadt / source / dfadt_prefs.c < prev    next >
C/C++ Source or Header  |  1996-04-07  |  4KB  |  206 lines

  1. /* $Revision Header built automatically *************** (do not edit) ************
  2. **
  3. ** © Copyright by Dirk Federlein
  4. **
  5. ** File             : Work:programming/dfa/Sourcen_V2.3/Datatypes/dfadt_prefs.c
  6. ** Created on       : Freitag, 17.03.95 22:56:44
  7. ** Created by       : Dirk Federlein
  8. ** Current revision : V1.0
  9. **
  10. **
  11. ** Purpose
  12. ** -------
  13. **     Reads the env:DFADT.prefs file.
  14. **
  15. ** Revision V1.0
  16. ** --------------
  17. ** created on Freitag, 17.03.95 22:56:44  by  Dirk Federlein.   LogMessage :
  18. **     --- Initial release ---
  19. **
  20. *********************************************************************************/
  21.  
  22. #include "all_includes.h"
  23. #include "dfadt.h"
  24.  
  25.  
  26. /*
  27.  
  28. Format:
  29.  
  30. FIELDNAME FIELDDESC FIELDLENGTH
  31.  
  32. where FIELDNAME may be any of
  33.     SALUTATION
  34.     FIRSTNAME
  35.     NAME
  36.     CO
  37.     STREET
  38.     ZIP
  39.     CITY
  40.     STATE
  41.     COUNTRY
  42.     BIRTHDAY
  43.     PHONE
  44.     FAX
  45.     EMAIL1
  46.     EMAIL2
  47.     EMAIL3
  48.     COMMENT
  49.  
  50. FIELDDESC may be any string and is meant to describe the contents of the field.
  51. Please notice that the datatype does NOT insert any spaces between the columns, 
  52. so if you want nothing but the field CONTENTS but e.g. ONE SPACE between the
  53. columns, you have to set the FIELDDESC of all but the first field to " ". The 
  54. first field should get a "".
  55.  
  56. */
  57.  
  58. #define ARGTEMPLATE "FieldName/A,FieldDescription/A,FieldLength/A/N"
  59.  
  60. static char linebuffer[1024];
  61.  
  62. static char * FIELDNAMES[] =
  63. {    "SALUTATION",
  64.     "FIRSTNAME",
  65.     "NAME",
  66.     "CO",
  67.     "STREET",
  68.     "ZIP",
  69.     "CITY",
  70.     "STATE",
  71.     "COUNTRY",
  72.     "BIRTHDAY",
  73.     "PHONE",
  74.     "FAX",
  75.     "EMAIL1",
  76.     "EMAIL2",
  77.     "EMAIL3",
  78.     "COMMENT",
  79.     NULL
  80. };
  81.  
  82. static char * DEFAULT_DELIM = " ";
  83.  
  84. // #define DEBUG
  85. #ifdef DEBUG
  86. #define D(x) x
  87. void kprintf(const char * fmt,...);
  88. #else
  89. #define D(x)
  90. #endif
  91.  
  92. struct FIELDDESC * dfadt_readprefs(struct DDTData * dd)
  93. {
  94.     BPTR                fp;
  95.  
  96.     char                * lineptr;
  97.  
  98.     long                i,k;
  99.     long                result[3]    = { 0 };
  100.  
  101.     struct EasyStruct ErrorES =
  102.     {
  103.         sizeof (struct EasyStruct), 0, NULL, NULL, NULL
  104.     };
  105.  
  106.     struct FIELDDESC    * fdesc    = NULL;
  107.  
  108.     struct RDArgs        * rdargs    = NULL;
  109.     struct RDArgs        fakerdargs    = {0};
  110.  
  111.     D(kprintf("dfadt_readprefs():\n"));
  112.     D(kprintf("dfadt_readprefs(): DDTData = 0x%lx\n", dd));
  113.     D(kprintf("dfadt_readprefs(): DDTData->ddt_Pool = 0x%lx\n", dd->ddt_Pool));
  114.  
  115.     if (fp = Open(DFADTPREFS, MODE_OLDFILE))
  116.     {
  117.  
  118.         D(kprintf("dfadt_readprefs(): PrefsFile = 0x%lx\n", fp));
  119.  
  120.         if (fdesc = AllocPooled(dd->ddt_Pool, 20*sizeof(struct FIELDDESC)))
  121.         {
  122.             D(kprintf("dfadt_readprefs(): fdesc = 0x%lx\n", fdesc));
  123.  
  124.             k    = 0;
  125.  
  126.             while ((k<19) && (lineptr = FGets(fp, linebuffer, 1023)))
  127.             {
  128.                 D(kprintf("dfadt_readprefs(): k = %ld\n", k));
  129.  
  130.                 fakerdargs.RDA_Source.CS_Buffer    = lineptr;
  131.                 fakerdargs.RDA_Source.CS_Length    = strlen(lineptr);
  132.                 fakerdargs.RDA_Source.CS_CurChr    = 0;
  133.  
  134.                 result[0] = result[1] = result[2] = 0;
  135.  
  136.                 if (rdargs = ReadArgs(ARGTEMPLATE, result, & fakerdargs))
  137.                 {
  138.                     D(kprintf("dfadt_readprefs(): result[0] = <%s>\n", (char *) result[0]));
  139.                     D(kprintf("dfadt_readprefs(): result[1] = <%s>\n", (char *) result[1]));
  140.                     D(kprintf("dfadt_readprefs(): result[2] = %ld\n", * ((ULONG*)( result[2]))));
  141.  
  142.                     if (result[0]) // description
  143.                     {
  144.                         for (i=0; i<16; i++)
  145.                         {
  146.                             if (stricmp((char *)result[0], FIELDNAMES[i]) == 0)
  147.                                 fdesc[k].offset    = i;
  148.                         }
  149.                     }
  150.  
  151.                     if (result[1])
  152.                     {
  153.                         if (fdesc[k].description = AllocPooled(dd->ddt_Pool, strlen((char*)result[1])+1))
  154.                             strcpy(fdesc[k].description, (char*)result[1]);
  155.                         else
  156.                             fdesc[k].description = DEFAULT_DELIM;
  157.  
  158.                     }
  159.  
  160.                     if (result[2])
  161.                     {
  162.                         if (result[2] > 0)
  163.                             fdesc[k].length = * ((ULONG*) (result[2]));
  164.                         else
  165.                             fdesc[k].length = 10;
  166.  
  167.                     }
  168.  
  169.                     FreeArgs(rdargs);
  170.                 }
  171.                 else
  172.                 {
  173.                     ErrorES.es_Title            = "DFA Datatype - Syntax Error";
  174.                     ErrorES.es_TextFormat        = "Syntax Error in line %ld\nof the preferences file.\nUsing default format...";
  175.                     ErrorES.es_GadgetFormat        = "Ok";
  176.  
  177.                     EasyRequest(NULL, &ErrorES, NULL, k+1);
  178.  
  179.                     FreePooled(dd->ddt_Pool, fdesc, 20*sizeof(struct FIELDDESC));
  180.  
  181.                     fdesc = NULL;
  182.  
  183.                     goto end;
  184.                 }
  185.  
  186.                 k++;
  187.             }
  188.  
  189.             D(kprintf("dfadt_readprefs(): (last) k = %ld\n", k));
  190.  
  191.             fdesc[k].offset = -1L;
  192.             fdesc[k].description = NULL;
  193.             fdesc[k].length = 0;
  194.  
  195. end:
  196.  
  197.             ;
  198.  
  199.         }
  200.  
  201.         Close(fp);
  202.     }
  203.  
  204.  
  205.     return(fdesc);
  206. }